﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace AsTekMobile.Controls
{
    public class GridColumn
    {
        public string Text
        {
            get
            {
                return Style.HeaderText;
            }
            set
            {
                if (Style != null)
                {
                    Style.HeaderText = value;
                    Event.Invoke(Style, null);
                }
            }
        }
        public int Width
        {
            get
            {
                return Style.Width;
            }
            set
            {
                if (Style != null)
                    Style.Width = value;
            }
        }
        public bool Visible
        {
            get
            {
                if (Style != null)
                {
                    if (Style.Width > -1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            set
            {
                if (!value)
                {
                    if (Style != null)
                        Style.Width = -1;
                }
            }
        }
        public DataGridColumnStyle Style { get; set; }
        public EventHandler Event { get; set; }
    }
    public class GridView : System.Windows.Forms.DataGrid, System.ComponentModel.ISupportInitialize
    {
        private List<GridColumn> _columns = new List<GridColumn>();
        public List<GridColumn> Columns { get { return _columns; } }
        public object VeriKaynagi
        {
            get { return this.DataSource; }
            set
            {
                this.SuspendLayout();
                if (this.TableStyles.Count > 0)
                    this.TableStyles[0].GridColumnStyles.Clear();
                this.TableStyles.Clear();
                _columns.Clear();
                this.DataSource = null;

                if (value != null)
                {
                    if (value is DataTable)
                    {
                        this.DataSource = value;
                        DataTable dt = (DataTable)value;
                        Graphics g = this.CreateGraphics();
                        DataGridTableStyle gridStyle = new DataGridTableStyle();
                        gridStyle.MappingName = dt.TableName;
                        int idx = 0;
                        foreach (DataColumn c in dt.Columns)
                        {
                            DataGridColumnStyle col = new DataGridTextBoxColumn();
                            col.MappingName = c.Caption;
                            col.HeaderText = c.Caption;
                            col.Width = 2 * (int)g.MeasureString(c.Caption, this.Font).Width;

                            _columns.Add(new GridColumn() { Text = col.HeaderText, Visible = true, Width = col.Width, Style = col, Event = AutoSizeColumn });
                            gridStyle.GridColumnStyles.Add(col);
                            idx++;
                        }
                        this.TableStyles.Add(gridStyle);
                        g.Dispose();
                        this.ResumeLayout();
                    }
                    else
                    {
                        Type t = value.GetType();
                        PropertyInfo p = t.GetProperty("Item");
                        int count = -1;
                        if (t.GetProperty("Count") != null &&
                            t.GetProperty("Count").PropertyType == typeof(System.Int32))
                        {
                            count = (int)t.GetProperty("Count").GetValue(value, null);
                        }
                        if (count > 0)
                        {
                            this.DataSource = value;
                            object val = p.GetValue(value, new object[] { 0 });

                            Graphics g = this.CreateGraphics();
                            DataGridTableStyle gridStyle = new DataGridTableStyle();
                            gridStyle.MappingName = t.Name;
                            int idx = 0;
                            foreach (PropertyInfo propertyInfo in val.GetType().GetProperties())
                            {
                                DataGridColumnStyle col = new DataGridTextBoxColumn();
                                col.MappingName = propertyInfo.Name;
                                col.HeaderText = propertyInfo.Name;
                                col.Width = 2 * (int)g.MeasureString(propertyInfo.Name, this.Font).Width;

                                _columns.Add(new GridColumn() { Text = col.HeaderText, Visible = true, Width = col.Width, Style = col, Event = AutoSizeColumn });
                                gridStyle.GridColumnStyles.Add(col);
                                idx++;
                            }
                            this.TableStyles.Add(gridStyle);
                            g.Dispose();
                        }
                        this.ResumeLayout();
                    }
                }
            }
        }

        private void AutoSizeColumn(object sender, EventArgs e)
        {
            DataGridColumnStyle st = (DataGridColumnStyle)sender;
            Graphics g = this.CreateGraphics();
            st.Width = 2 * (int)g.MeasureString(st.HeaderText, this.Font).Width;
            g.Dispose();
        }

        public int RowCount
        {
            get
            {
                if (this.DataSource is DataTable)
                {
                    return ((DataTable)this.DataSource).Rows.Count;
                }
                else
                {
                    Type t = this.DataSource.GetType();
                    PropertyInfo p = t.GetProperty("Item");
                    int count = 0;
                    if (t.GetProperty("Count") != null &&
                        t.GetProperty("Count").PropertyType == typeof(System.Int32))
                    {
                        count = (int)t.GetProperty("Count").GetValue(this.DataSource, null);
                    }
                    return count;
                }
            }
        }

        #region ISupportInitialize Members
        public void BeginInit()
        {

        }
        public void EndInit()
        {
        }
        #endregion
    }
}
